const express = require('express')
const passport = require('passport')
const expressJoi = require('@escook/express-joi')
const bcrypt = require('bcrypt')
const db = require('../../db')
const router = express.Router()

const {employees_updatepwd_schema, employees_id_schema} = require('../../schema/employees')

//获取所有员工信息(管理员)
router.get('/',passport.authenticate('jwt', {session: false}) ,(req, res) => {
    if(req.user.role !== 1) return res.json({status: 1, message: '非管理员，无法获取所有员工信息'})
    const sqlStr = 'select id, name, email, avatar, role, date from users'
    db.query(sqlStr, (err, results) => {
        if(err) return res.json({status: 1, message: '执行sql语句失败'})
        if(results.length === 0) return res.json({status: 1, message: '无员工信息'})
        res.json(results)
    })
})
//根据ID获取员工信息(管理员)
router.get('/userinfo/:id', expressJoi(employees_id_schema), passport.authenticate('jwt', {session: false}) , (req, res) => {
    if(req.user.role !== 1) return res.json({status: 1, message: '非管理员，无法获取该员工信息'})
    const sqlStr = 'select id, name, email, avatar, role, date from users where id = ?'
    db.query(sqlStr, req.params.id, (err, results) => {
        if(err) return res.json({status: 1, message: '执行sql语句失败'})
        if(results.length === 0) return res.json({status: 1, message: '暂无此员工信息'})
        res.json(results[0])
    })
})
//获取个人信息
router.get('/userinfo', passport.authenticate('jwt', {session: false}), (req, res) => {
    const sqlStr = 'select id, name, email, avatar, role, date from users where id = ?'
    db.query(sqlStr, req.user.id, (err, results) => {
        if(err) return res.json({status: 1, message: '执行sql语句失败'})
        if(results.length !== 1) return res.json({status: 1, message: '信息获取失败'})
        res.json(results[0])
    })
})
//修改个人信息(name, email)
router.post('/edit', passport.authenticate('jwt', {session: false}), (req, res) => {
    if(req.body.name === req.user.name) return res.json({status: 1, message: '姓名与原来一致'})
    if(req.body.email === req.user.email) return res.json({status: 1, message: '邮箱与原来一致'})
    const sql = 'select * from users where id <> ? and email = ?'
    db.query(sql, [req.user.id, req.body.email], (err, results) => {
        if(err) return res.json({status: 1, message: '执行sql语句失败'})
        if(results.length > 0) return res.json({status: 1, message: '邮箱已被占用'})
        const sqlStr = 'update users set ? where id = ?'
        const userFileds = {}
        if(!req.body.name && !req.body.email) return res.json({status: 1, message: '不能全为空'})
        if(req.body.name) userFileds.name = req.body.name
        if(req.body.email) userFileds.email = req.body.email
        db.query(sqlStr, [userFileds, req.user.id], (err, results) => {
            if(err) return res.json({status: 1, message: '执行sql语句失败'})
            if(results.affectedRows !== 1) return res.json({status: 1, message: '修改失败'})
            res.json({status: 0, message: '修改成功'})
        })
    })
})
//重置密码
router.post('/resetpwd', passport.authenticate('jwt', {session: false}), expressJoi(employees_updatepwd_schema), (req, res) => {
    if(req.body.oldpassword === req.body.newpassword) return res.json({status: 1, message: '新密码不能与原密码一致'})
    const sqlStr = 'select * from users where id = ?'
    db.query(sqlStr, req.user.id, (err, results) => {
        if(err) return res.json({status: 1, message: '执行sql语句失败'})
        if(results.length !== 1) return res.json({status: 1, message: '用户不存在'})
        bcrypt.compare(req.body.oldpassword, results[0].password, (err, result) => {
            if(err) throw err
            if(result){
                bcrypt.genSalt(10, function(err, salt) {
                    bcrypt.hash(req.body.newpassword, salt, function(err, hash) {
                        if(err) throw err
                        const sql = 'update users set password = ? where id = ?'
                        db.query(sql, [hash, req.user.id], (err, results) => {
                            if(err) return res.json({status: 1, message: '执行sql语句失败'})
                            if(results.affectedRows !== 1) return res.json({status: 1, message: '修改失败'})
                            res.json({status: 0, message: '重置成功'})
                        })
                    });
                });
            }else {
                res.json({status: 1, message: '原密码错误'})
            }
        })
    })
})

module.exports = router